/* Copyright 2009 Sony Mathew, Minneapolis MN. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package com.xsm.gwt.widgets.client;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;

/**
 * A panel that provides a minimum width and/or height for its content.
 * 
 * Min Sizes can ONLY be controlled via CSS - to do so :
 * 
 * Specify an ID or CSS class directly for this panel or an Ancestor panel and then
 *         idOfPanelOrAncestor .minWidthSizer
 *         idOfPanelOrAncestor .minHeightSizer
 *         
 * Or you can add a CSS class name directly for min width or height sizing:
 *      {@link #addMinWidthSizerStyleName(String)}
 *      {@link #addMinHeightSizerStyleName(String)}
 *      
 * 
 * @author Sony Mathew
 */
public class MinSizePanel extends SimplePanel {

    private SimplePanel contentHolder = null;
    private Widget minWidthSizer = null;
    private Widget minHeightSizer = null;
    private FlexTable minSizerLayout = null;
    private int minWidthSizerCellRow = -1;
    private int minWidthSizerCellColumn = -1;
    
    /**
     * author Sony Mathew
     */
    public MinSizePanel() {
        build();
    }
    
    /**
     * XXX: Sony: Should be using the newer BorderLayoutPanel ??
     * 
     * author Sony Mathew
     */
    private void build() {
        addStyleName("minSizer");
        
        contentHolder = new SimplePanel();
        contentHolder.setStyleName("contentHolder");

        //table, cell and the width sizer itself share this so as to style width across all.
        final String minWidthSizerClass = "minWidthSizer";
        
        minWidthSizer = new SimplePanel();        
        minWidthSizer.setStyleName(minWidthSizerClass);
        minWidthSizer.getElement().getStyle().setProperty("lineHeight", "0");
        minWidthSizer.getElement().getStyle().setProperty("height", "0");
        
        minHeightSizer = new SimplePanel();
        minHeightSizer.setStyleName("minHeightSizer");
        minHeightSizer.getElement().getStyle().setProperty("lineHeight", "0");
        minHeightSizer.getElement().getStyle().setProperty("width", "0");
        
        minSizerLayout = new FlexTable();
        minSizerLayout.setStyleName("minSizerLayout");        
        minSizerLayout.setCellPadding(0);
        minSizerLayout.setCellSpacing(0);
        
        minSizerLayout.setWidget(0,0, new SimplePanel());
        minSizerLayout.setWidget(minWidthSizerCellRow=0, minWidthSizerCellColumn=1, minWidthSizer);
        minSizerLayout.setWidget(1,0, minHeightSizer);
        minSizerLayout.setWidget(1,1, contentHolder);   
        
        //Width sizing also sizes the cell and the table as well.
        minSizerLayout.getFlexCellFormatter().setStyleName(minWidthSizerCellRow, minWidthSizerCellColumn, minWidthSizerClass);
        minSizerLayout.addStyleName(minWidthSizerClass);
        
        /* Allows precise Pixel sizing but then EM based sizing fails.
        minSizer.getFlexCellFormatter().getElement(0,1).getStyle().setFontSize(0.0, Unit.PX);
        minSizer.getFlexCellFormatter().getElement(1,0).getStyle().setFontSize(0.0, Unit.PX);
        */
        
        //use super, cause setWidget() here has been overridden to setContent.
        super.setWidget(minSizerLayout);        
    }

    /**
     * author Sony Mathew
     */
    public Widget getContent() {
        return contentHolder.getWidget();
    }

    /**
     * Set the content of this panel that sizes to minimum size.
     * 
     * author Sony Mathew
     */
    public void setContent(Widget content) {
        this.contentHolder.setWidget(content);
    }

    
    /**
     * Same as setContent(Widget)
     * 
     * @see com.google.gwt.user.client.ui.SimplePanel#setWidget(com.google.gwt.user.client.ui.Widget)
     */
    @Override
    public void setWidget(Widget w) {
        setContent(w);
    }


    /**
     * Returns the content.
     * 
     * @see com.google.gwt.user.client.ui.SimplePanel#getWidget()
     */
    @Override
    public Widget getWidget() {
        return this.contentHolder.getWidget();
    }

    /**
     * Same as setContent(Widget)
     */    
    @Override
    public void add(Widget w) {
        setContent(w);
    }

    /**
     * Sets the horizontal alignment of the content within Min Sizer panel.
     * 
     * @see HasHorizontalAlignment#setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentConstant)
     */
    public void setHorizontalAlignment(HorizontalAlignmentConstant align) {
        minSizerLayout.getFlexCellFormatter().setHorizontalAlignment(1,1, align);
    }

    /**
     * Sets the vertical alignment of the content within Min Sizer panel.
     * panel. It only applies to widgets added after this property is set.
     * 
     * @see HasVerticalAlignment#setVerticalAlignment(HasVerticalAlignment.VerticalAlignmentConstant)
     */
    public void setVerticalAlignment(VerticalAlignmentConstant align) {
        minSizerLayout.getFlexCellFormatter().setVerticalAlignment(1,1, align);
    }

    /**
     * Add another style Name for the minWidthSizer So you can size it.
     *  This will be added to the entire heirachy of widgets including the root MinSizePanel. 
     *      So that width control affects all at the same time. 
     * 
     * author Sony Mathew
     */
    public void addMinWidthSizerStyleName(String minWidthSizerStyleName) {
        addStyleName(minWidthSizerStyleName);
        minSizerLayout.addStyleName(minWidthSizerStyleName);
        minSizerLayout.getFlexCellFormatter().setStyleName(minWidthSizerCellRow, minWidthSizerCellColumn, minWidthSizerStyleName);
        minWidthSizer.addStyleName(minWidthSizerStyleName);
    }
    
    /**
     * Add another style Name for the minHeightSizer So you can size it.
     * 
     * author Sony Mathew
     */
    public void addMinHeightSizerStyleName(String minWidthSizerStyleName) {
        minHeightSizer.addStyleName(minWidthSizerStyleName);
    }
    
}
